package com.abigdreamer.im.server.ui;



import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;

import com.abigdreamer.im.server.log.EIMLogger;
import com.abigdreamer.im.server.sound.SoundPlay;
import com.abigdreamer.im.server.ui.resourse.ResourseMannager;


/**
 * CMPP v1.1.0版本
 * 
 * CMpp的桌面右下角的系统托盘类
 * 
 */
public class EIMTrayIcon extends BaseFrame implements Runnable {
	
	/**托盤的圖標	 */
	public String sysTeamTrayIconName="service_closed.png";
	/**托盤的圖標是否改变	 */
	public boolean imageIsChange=true;
	
    /**CMpp系统主界面*/
	private ServerMainFrame mainFrame;
	
	private static EIMTrayIcon cmppISMGDeskTopIcon;
	
	private SystemTray tray;
	
	private TrayIcon trayIcon;
	
	private Image image;
	
	private EIMTrayIcon(){
		
	}
	
	public static EIMTrayIcon getInstanse(){
		if(null==cmppISMGDeskTopIcon){
			cmppISMGDeskTopIcon=new EIMTrayIcon();
		}
		return cmppISMGDeskTopIcon;
	}
	
	public void run() {
		initTrayIcon(sysTeamTrayIconName);
	}
	/**
	 * 启动cmpp服务器的系统托盘，初始化托盘的参数
	 * @param trayIconName
	 */
	private void initTrayIcon(String trayIconName){
		/**
		 * 判断操作系统是否支持系统托盘
		 * 
		 * 1：支持则现实 xp
		 * 2：不支持则采取别的方式 unix
		 */
		if(SystemTray.isSupported()){
			
			/** 通过静态方法getSystemTray()得到系统托盘 */
		    tray = SystemTray.getSystemTray(); 
			/** 装载托盘图象 */
		    image =this.getTrayIconImage(trayIconName);
		    /** 为这个托盘加一个弹出菜单 */
		    PopupMenu popup =this.initTrayIconPopupMenu();
			/** 为这个托盘加一个提示信息*/		  
			
		    trayIcon = new TrayIcon(image, "企业即时通讯(EIM)服务器", popup); 
		    
		    trayIcon.addMouseListener(new java.awt.event.MouseAdapter(){
				   public void mouseClicked(MouseEvent e){
					  if(e.getClickCount() == 2){
						  mainFrame=ServerMainFrame.getInstance();
							   if(mainFrame.isVisible()){
								   mainFrame.setVisible(false);
							   }
							   else{
								   mainFrame.setVisible(true);
					    }
					  }
			           }}); 
			try 
			 {  
				tray.add(trayIcon); 
				ToolTip.startBubbleTip("海亮-企业即时通讯服务器！！");
				EIMLogger.INFO("向这个托盘添加新项： " );
			  } 
			  catch (AWTException e) 
			    { 
				  EIMLogger.INFO("无法向这个托盘添加新项： " + e);
				} 
			  
		}else{
			EIMLogger.INFO("无法使用系统托盘!");
		}
	}
	
	/**
	 * 通过托盘的图标得到这个图片对象
	 * @param trayIconName：这个图标的名字
	 * @return：还回图标对象
	 */
	private Image getTrayIconImage(String trayIconName){
		java.net.URL url=ResourseMannager.class.getResource(trayIconName);
	    Image image = Toolkit.getDefaultToolkit().getImage(url);
	    return image;
	}
	
	/**
	 * 初始化系统托盘的菜单
	 * 1：初始化系统托盘操作
	 * 2：添加些事件
	 * @return
	 */
	private PopupMenu initTrayIconPopupMenu(){
		PopupMenu popup = new PopupMenu(); 
		MenuItem myMenu = new MenuItem("系统主页"); 
		MenuItem exitItem = new MenuItem("系统退出");
		MenuItem systemConfig = new MenuItem("系统配置");
		MenuItem systemTools = new MenuItem("系统工具");
		
		popup.add(myMenu); 
		popup.add(systemConfig); 
		popup.add(systemTools);
		popup.add(exitItem); 
		
		exitItem.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)     {   
                try{     
                	 SoundPlay.playClosedSound();
                     System.exit(0);     
                  }catch(Exception   ex)   {   
                      ex.printStackTrace();   
                  }   
            }
        });  
		/**
		 * 系统主页的事件
		 */
		myMenu.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)     {
            }
        });  
		return popup;
	}
	
	public void changeIconIamge(String evt){
		
		if("start".equals(evt)){
		    image=this.getTrayIconImage("service_ok.png");
			trayIcon.setImage(image);
			ToolTip.startBubbleTip("服务器已经启动！！");
			SoundPlay.playNotifySound();
			
		}
		else if("closed".equals(evt)){
		    image=this.getTrayIconImage("service_closed.png");
			trayIcon.setImage(image);
			ToolTip.startBubbleTip("服务器已经停止！！");
			SoundPlay.playNotifySound();
		}
		else if("refresh".equals(evt)){
		    image=this.getTrayIconImage("service_refresh.png");
			trayIcon.setImage(image);
			ToolTip.startBubbleTip("服务器进入刷新阶段！！");
			SoundPlay.playNotifySound();
			
		}
	}
	
	

	
}
